/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package protocol;


import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;


public class Packet implements Serializable{

    private String idPacket;
    private InetSocketAddress sender = null;
    private InetSocketAddress receiver = null;
    private int messageType;
    private Object message;
    private int ttl;
    private int hops;
 


    private Packet(int messageType, Object message){
        try{
      MessageDigest md = MessageDigest.getInstance("MD5");
         byte []idPacketByte = md.digest((sender + String.valueOf(messageType) + String.valueOf(System.currentTimeMillis())).getBytes());
         idPacket = getHexString(idPacketByte);
         this.messageType = messageType;
         this.message = message;
         ttl = 5;
         hops = 0;
        }catch(Exception e){
            System.err.println("errore generazione pacchetto");
        }
    }



    public Packet(InetSocketAddress sender, InetSocketAddress receiver, int messageType, Object message){
        this(messageType, message);
        this.sender = sender;
        this.receiver = receiver;

    }

    public Packet(InetSocketAddress sender, int messageType, Object message){
      this(messageType, message);
      this.sender = sender;
    }

    public Packet (InetSocketAddress sender, InetSocketAddress receiver, int messageType, Object message, int ttl){
        this(sender, receiver, messageType, message);
        this.ttl = ttl;
    }

    public Packet(Packet p){
        this.idPacket = p.idPacket;
        this.messageType = p.messageType;
        this.sender = p.sender;
        this.receiver = p.receiver;
        this.ttl = p.ttl;
        this.hops = p.hops;
        this.message = p.message;
    }

    public int getHops() {
        return hops;
    }

    public void setHops(int hops) {
        this.hops = hops;
    }

    public Object getMessage() {
        return message;
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public int getMessageType() {
        return messageType;
    }



    public InetSocketAddress getReceiver() {
        return receiver;
    }

    public void setReceiver(InetSocketAddress receiver) {
        this.receiver = receiver;
    }

    public InetSocketAddress getSender() {
        return sender;
    }

    public void setSender(InetSocketAddress sender) {
        this.sender = sender;
    }

    public int getTtl() {
        return ttl;
    }

    public void setTtl(int ttl) {
        this.ttl = ttl;
    }

    public String getGUID(){
        return idPacket;
    }

    private static String getHexString(byte[] b) {
         String result = "";
            for (int i=0; i < b.length; i++) {
            result +=
                Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
    }
    return result;
}

    public String toString(){

        String str="";
        if(messageType==1) str = "PING ";
        str += "GUID " + idPacket;
        str += sender.getAddress()+ ":" +sender.getPort() + " ttl "+ ttl +" hops  "+ hops ;
        if(message instanceof Vector){
            return str += " message " +message;
        }


        return str;

    }



    public void incrementHops(){
        ttl=ttl-1;
        hops = hops+1;
    }


}
